﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogre;

namespace WastedMechanics.CharState
{
    public abstract class CharStateRun : CharState
    {        
        protected float RunSpeedScale;
        protected string AnimationName;

        public CharStateRun(Character chr, string animName)
            : base(chr)
        {            
            RunSpeedScale = 1;
            AnimationName = animName;
        }

        public override void Enter()
        {
            Character.SetAnimation(AnimationName);  
            Character.CanTurn = true;
            Character.IsMoving = true;
            if (Character.SoundFootStep == null && Character.Profile.SoundFootstepFx != null)
            {
                Character.SoundFootStep = Character.Profile.SoundFootstepFx.PlayNewInstance(true);
                Character.SoundFootStep.Volume = 0.3f;
                Character.SoundFootStep.SetPosition(Character.Position);
            }
        }

        public override void Exit()
        {
            Character.IsMoving = false;
            if (Character.SoundFootStep != null)
            {
                Character.SoundFootStep.Stop();
                Character.SoundFootStep = null;
            }
        }

        public override void HaltOrder()
        {
            Character.SwitchState(GetIdleState());
        }            

        public override void Update()
        {
            if (Character.SoundFootStep != null)
                Character.SoundFootStep.SetPosition(Character.Position);

            Character.Velocity = (Character.Orientation * Vector3.UNIT_Z * Character.Profile.RunSpeed * RunSpeedScale) * Character.RunAcceleration;
            Character.RunAcceleration = Helper.Clamp(0, 1, Character.RunAcceleration + 0.1f);
            Character.AnimBlender.TimeScale = 1.7f * Character.Velocity.Length / (Character.Profile.RunSpeed * RunSpeedScale);            

            if (!Character.IsOnGround || Character.Body.Velocity.y < -3)
                Character.SwitchState(GetFallingState());
        }

        public abstract CharState GetIdleState();
        public abstract CharState GetFallingState();
    }
}
